package 树;
/**
 * https://leetcode-cn.com/problems/symmetric-tree/
 */
public class _101_对称二叉树 {
    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
    }
    //函数定义:给定一个根节点为root的二叉树，判断是否是对称(左右都右子节点)
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;

        return check(root.left,root.right);
    }

    //函数定义:给定两个节点，判断是否相等
    private boolean check(TreeNode left,TreeNode right){
        if(left == null || right == null) return left == right; //递归边界条件

        //值不相等直接返回false
        if(left.val!=right.val) return false;

        //递归去处理每一对根节点是否相等
        return check(left.right,right.left) && check(left.left,right.right);
    }
}
